package com.example.hehecat.yuejian1.ui.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.hehecat.yuejian1.R;
import com.example.hehecat.yuejian1.mvp.contract.MasterInfoContract;
import com.example.hehecat.yuejian1.mvp.model.MasterInfoModel;
import com.example.hehecat.yuejian1.mvp.model.api.Api;
import com.example.hehecat.yuejian1.mvp.model.bean.MasterBean;
import com.example.hehecat.yuejian1.mvp.model.bean.MasterLessonBean;
import com.example.hehecat.yuejian1.mvp.presenter.MasterInfoPresenter;
import com.example.hehecat.yuejian1.ui.activity.ReplyActivityActivity;
import com.example.hehecat.yuejian1.ui.activity.ReplyMasterActivity;
import com.example.hehecat.yuejian1.ui.adapter.ActivityLessonListAdapter;
import com.example.hehecat.yuejian1.ui.wiget.MyItemDecoration;
import com.example.hehecat.yuejian1.ui.wiget.MyStaticVariable;
import com.ldoublem.thumbUplib.ThumbUpView;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;
import rx.Subscriber;

import static com.chad.library.adapter.base.listener.SimpleClickListener.TAG;

public class MasterInfoFragment extends SupportFragment implements MasterInfoContract.View {


    Unbinder unbinder;
    @BindView(R.id.masterinfo_return)
    ImageView masterinfoReturn;
    @BindView(R.id.TextView)
    android.widget.TextView TextView;
    @BindView(R.id.master_share)
    Button masterShare;
    @BindView(R.id.master_tpv)
    ThumbUpView masterTpv;
    @BindView(R.id.master_img)
    ImageView masterImg;
    @BindView(R.id.title)
    AppCompatTextView title;
    @BindView(R.id.masterinfo_intro)
    AppCompatTextView masterinfoIntro;
    @BindView(R.id.masterinfo_phone)
    android.widget.TextView masterinfoPhone;
    @BindView(R.id.masterinfo_sex)
    android.widget.TextView masterinfoSex;
    @BindView(R.id.master_consult)
    Button masterConsult;
    @BindView(R.id.master_lessons)
    RecyclerView masterLessons;
    private int user_id;
    private int master_id;
    ActivityLessonListAdapter adapter;
    MasterInfoPresenter presenter;

    @OnClick(R.id.master_share)
    void masterShare() {
        Intent textIntent = new Intent(Intent.ACTION_SEND);
        textIntent.setType("text/plain");
        textIntent.putExtra(Intent.EXTRA_TEXT, masterinfoIntro.getText());
        startActivity(Intent.createChooser(textIntent, "分享"));
    }

    @OnClick(R.id.masterinfo_return)
    void masterReturn() {
        MainFragment fragment = new MainFragment();
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content, fragment, null)
                .commit();
    }

    @OnClick(R.id.master_consult)
    void masterReply() {
        Intent intent = new Intent(getContext(), ReplyMasterActivity.class);
        intent.putExtra("master_id", master_id);
        startActivity(intent);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_master_info, container, false);
        unbinder = ButterKnife.bind(this, view);

        if (getArguments() != null) {
            master_id = getArguments().getInt("master_id");
        }
        user_id = MyStaticVariable.newInstance().getUserid();

        presenter = new MasterInfoPresenter(this);
        presenter.updateAdapterData(master_id);

        //点赞
        isMasterCollect();
        masterTpv.setCracksColor(Color.rgb(22, 33, 44));
        masterTpv.setFillColor(Color.rgb(11, 200, 77));
        masterTpv.setEdgeColor(Color.rgb(33, 3, 219));
        masterTpv.setOnThumbUp(like -> collectMaster());

        //获取详情
        MasterInfoModel model = new MasterInfoModel();
        model.getMaster(master_id, new Subscriber<MasterBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "onError: "+e.getMessage());
            }

            @Override
            public void onNext(MasterBean activityBean) {
                if (activityBean.getMastersex()==1){
                    masterinfoSex.setText("男");
                }else{
                    masterinfoSex.setText("女");
                }
                masterinfoPhone.setText(activityBean.getMasterphone());
                masterinfoIntro.setText(activityBean.getMasterintro());
                Picasso.with(getContext()).load(Api.IMG_URL + activityBean.getMasterpic()).into(masterImg);
            }
        });

        //创建布局管理
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        masterLessons.setLayoutManager(layoutManager);
        masterLessons.addItemDecoration(new MyItemDecoration());

        //创建适配器
        adapter = new ActivityLessonListAdapter(R.layout.item_list, getContext());
        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        adapter.setNotDoAnimationCount(2);

        //给RecyclerView设置适配器
        masterLessons.setAdapter(adapter);
        return view;
    }

    public static MasterInfoFragment newInstance(int master_id) {
        MasterInfoFragment fragment = new MasterInfoFragment();
        Bundle args = new Bundle();
        args.putInt("master_id", master_id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void showMsg(String msg) {
        Toast.makeText(_mActivity, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setCollect() {
        masterTpv.setLike();
    }

    @Override
    public void setUnCollect() {
        masterTpv.setUnlike();
    }


    @Override
    public void updateAdapterData(List<MasterLessonBean> data) {
        adapter.addData(data);
    }

    @Override
    public void collectMaster() {
        presenter.collectMaster(user_id,master_id);
    }

    @Override
    public void isMasterCollect() {
        presenter.isCollected(master_id,user_id);
    }
}
